package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.huike.clues.service.ISysUserService;
import com.huike.clues.service.ITbAssignRecordService;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {
    //合同
    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    //线索
    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;
    //商机
    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ReportMapper reportMpper;
    @Autowired
    ISysUserService iSysUserService;
    @Autowired
    ITbAssignRecordService iTbAssignRecordService;



    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId = request.getDeptId();
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    //龙虎商机
    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
             int cc= businessMapper.selectTbBusiness2(request);
        List<Map<String, Object>> list = contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId = (Long) datum.get("dept_id");
            if (deptId != null) {
                SysDept dept = deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
            Long num = (Long) datum.get("num");
            BigDecimal radio = getRadio(cc, num);
            datum.put("radio",radio);

        }
        return list;
    }

    //龙虎线索
    @Override
    public List<Map<String, Object>> salesStatisticsForIndex1(IndexStatisticsVo request) {
        List<Map<String, Object>> list = businessMapper.contractStatisticsByUser1(request);

        for (Map<String, Object> datum : list) {
            String create_by = (String) datum.get("create_by");
            SysUser sysUser = iSysUserService.selectUserByUserName(create_by);
            Long deptId1 = sysUser.getDeptId();
            if (deptId1 != null) {
                SysDept dept = deptMapper.selectDeptById(deptId1);
                datum.put("deptName", dept.getDeptName());
            }
              int nums = iTbAssignRecordService.selectAssignRecordusername(create_by,"0");
            BigDecimal radio = getRadio(nums, (Long) datum.get("num"));
            datum.put("radio",radio);
        }
        return list;
    }

    //查询数据面板
    @Override
    public IndexVo getIndexs(String beginCreateTime, String endCreateTime) {
        int c = clueMapper.selectClue(beginCreateTime, endCreateTime);

        int d = businessMapper.selectTbBusiness(beginCreateTime, endCreateTime);

        int e = contractMapper.selectContract(beginCreateTime, endCreateTime);
        Float e1 = (Float) contractMapper.selectmoney(beginCreateTime, endCreateTime);
        BigDecimal bigDecimal = new BigDecimal(e1);
        double v = bigDecimal.doubleValue();
        IndexVo indexVo = new IndexVo();
        indexVo.setCluesNum(c); //线索数目
        indexVo.setBusinessNum(d);  //商机数目
        indexVo.setContractNum(e);  //合同数目
        indexVo.setSalesAmount(v);  //销售金额

        String date = DateUtils.getDate();
        int c1 = clueMapper.selectClue1(date);

        indexVo.setTodayCluesNum(c1);  //今日线索数目
        indexVo.setTodayBusinessNum(6); //今日商机数目
        indexVo.setTodayContractNum(7);  //今日合同数目
        indexVo.setTodaySalesAmount(8.0);   //今日销售金额

        //待办.
        indexVo.setTransferNum(9);   //转派
        indexVo.setTofollowedCluesNum(10);   //待跟进线索数目
        indexVo.setTofollowedBusinessNum(11);  //待跟进商机数目
        indexVo.setToallocatedCluesNum(12);   //待分配线索数目
        indexVo.setToallocatedBusinessNum(13);   //待分配商机数目
        return indexVo;
    }

    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }

}